
function setColor(temperature, tempmax, tempmin){
	
	var delta = tempmax + tempmin;
	
	var valeurRouge = temperature/ delta;
	var valeurBleu =  1- (temperature/delta);
	
	var color = new THREE.Color( 0xff0000 );
	
        
	color.setRGB(valeurRouge,0,valeurBleu);
	
	return color.getHex();
}
function setHueColor(temperature, tempmax, tempmin){
	
          var delta = Math.abs(tempmax) +  Math.abs(tempmin);  //angle rouge =0 bleu = 255
          
        var valCouleurHue = ((-255 * temperature) + (255*tempmax))/(tempmax-tempmin); //bleu -> froid/petit/faible
        
        var color = new THREE.Color( 0xff0000 );
        
        color.setHSV((1/360)*valCouleurHue, 1, 1);
	
	return color.getHex();
}

function createCube(temperature,posx,posy,posz,tempMax,tempMin,taille){
	
	var geometry = new THREE.CubeGeometry(taille,taille,taille);	
	
	var material = new THREE.MeshPhongMaterial({color:setHueColor(temperature,tempMax,tempMin)}); 
	
	var cube = new THREE.Mesh(geometry, material); 

	cube.position.x = 0.5 + posx;
	cube.position.y = 0.5 + posy;
	cube.position.z = 0.5 + posz;
	
	return cube;
}
function createSphere(temperature,posx,posy,posz,tempMax,tempMin,taille){
	
	var geometry =  new THREE.SphereGeometry(taille/2,20,20);	
	
	var material = new THREE.MeshPhongMaterial({color:setHueColor(temperature,tempMax,tempMin)}); 
	
	var cube = new THREE.Mesh(geometry, material); 

	cube.position.x = 0.5 + posx;
	cube.position.y = 0.5 + posy;
	cube.position.z = 0.5 + posz;
	
	return cube;
}

function afficherGrille(scene,largeur,hauteur,pas){
	var size = largeur * hauteur;
	var step = pas;

	var geometry = new THREE.Geometry();

		for ( var i = - size; i <= size; i += step ) {

					geometry.vertices.push( new THREE.Vector3(- size , 0, i ) );
					geometry.vertices.push( new THREE.Vector3( size, 0, i ) );

					geometry.vertices.push( new THREE.Vector3( i, 0, - size ) );
					geometry.vertices.push( new THREE.Vector3( i, 0, size) );

				}
	var material = new THREE.LineBasicMaterial( {color: 0x000000, opacity: 0.2} );

	var line = new THREE.Line( geometry, material );
	line.type = THREE.LinePieces;
	scene.add( line );

}

function updateViewFromReceivedData(echantillon)
{
    var tailleEchantillon = echantillon.size;
    var valeurMax = echantillon.dataMax.dataValue;
    var valeurMin = echantillon.dataMin.dataValue;
    
    for (i = 0; i < tailleEchantillon; i++) 
    { 
        var temperature = echantillon.datas[i].dataValue;
        
        var posx = echantillon.datas[i].posx;
        var posy = echantillon.datas[i].posy;
        var posz = echantillon.datas[i].posz;
        
        var vecteurx = (echantillon.dataMax.posx + echantillon.dataMin.posx)/2;
        var vecteurz = (echantillon.dataMax.posz + echantillon.dataMin.posz)/2;
        

        object = createSphere(temperature, posx -vecteurx, posy, posz-vecteurz, valeurMax, valeurMin, 0.8);
        object.value = echantillon.datas[i].dataValue;
        
        scene.add(object);  
        objects.push(object);

    } 

}